﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using YXShop.Web;
using YXShop.Model.Order;
using System.Data;

namespace YXShop.TemplateAction
{
    public partial class LabelParse
    {

        YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
        public string UserShoppingInfo()
        {
            string mystyle = this.Label_Inserted;
            string TempletLayout = this.GetParamValue("YX:TempletLayout");
            StringBuilder shtml = new StringBuilder();
            if (!string.IsNullOrEmpty(mystyle))
            {
                #region 收货人信息
                if (mystyle.IndexOf("[#ConsigneeInfo]") > -1)
                {
                    int uid = 0;
                    if (HttpContext.Current.Session["MemberID"] != null)
                    {
                        uid = Convert.ToInt32(HttpContext.Current.Session["MemberID"]);
                    }
                    string UID = string.Empty;
                    string UserName = string.Empty;
                    string AddressInfo = string.Empty;
                    string Tel = string.Empty;
                    string Fax = string.Empty;
                    string post = string.Empty;
                    string HomePhone = string.Empty;
                    string Email = string.Empty;
                    string Province = string.Empty;
                    string City = string.Empty;
                    string Borough = string.Empty;
                    string constructionSigns = string.Empty;
                    string consignesTime = string.Empty;
                    string fillConsignedata = "0";
                    string UserTypeId = ChangeHope.WebPage.PageRequest.GetQueryString("UserTypeId");
                    string UserId = ChangeHope.WebPage.PageRequest.GetQueryString("UserId");
                    string operate = "<a href=\"javascript:showPath('UserShoppingInfo',document.getElementById('address'),'填写收货人信息',700,285,'" + sp.DummyPaht + "','0||" + UserTypeId + "|" + UserId + "')\" title=\"点击填写收货人信息息\"><span style='color:#0000FF'>点击填写收货人信息</span></a>";
                    if (uid > 0)
                    {
                        YXShop.BLL.Member.ReceAddress rabll = new YXShop.BLL.Member.ReceAddress();
                        List<YXShop.Model.Member.ReceAddress> raList = rabll.GetAll(" uid=" + uid + " and stat=1");
                        if (raList.Count > 0)
                        {
                            fillConsignedata = "1";
                            UID = raList[0].UID.ToString();
                            UserName = raList[0].UserName;
                            AddressInfo = raList[0].Address;
                            HomePhone = raList[0].Phone;
                            Fax = raList[0].Fax;
                            post = raList[0].Zip;
                            Tel = raList[0].Mobile;
                            Email = raList[0].Email;
                            Province = raList[0].Province;
                            City = raList[0].City;
                            Borough = raList[0].Borough.ToString();
                            constructionSigns = raList[0].ConstructionSigns;
                            consignesTime = raList[0].ConsignesTime;
                            operate = "<a href=\"javascript:showPath('UserShoppingInfo',document.getElementById('address'),'修改收货人信息',700,285,'" + sp.DummyPaht + "','" + UID + "|" + raList[0].ID + "|" + UserTypeId + "|" + UserId + "')\" title=\"点击修改收货人信息息\"><span style='color:#0000FF'>修改</span></a>";
                        }
                    }
                    string SessionValue = string.Empty;
                    shtml.Append("<div id=\"address\"></div><div><input name=\"putoutId\" id=\"putoutId\" type=\"hidden\" value=\"" + UserId + "\" /><input name=\"putoutTypeId\" id=\"putoutTypeId\" type=\"hidden\" value=\"" + UserTypeId + "\" /></div>");
                    shtml.Append("<div id=\"ConsignesInfo\" class=\"ConsignesInfo\">");
                    shtml.Append("<table class=\"ConsignesTable\" align=\"center\" border=\"0\" cellpadding=\"5\" cellspacing=\"1\">");
                    shtml.Append("<tr>");
                    shtml.Append("<td class=\"ConsignesTd_Name\">收货人姓名：</td>");
                    shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userName\" id=\"userName\" readonly value=\"" + UserName + "\"></td>");
                    shtml.Append("<td class=\"ConsignesTd_Name\">电子邮件地址：</td>");
                    shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userEmail\" id=\"userEmail\" readonly value=\"" + Email + "\"></td>");
                    shtml.Append("</tr>");
                    shtml.Append("<tr>");
                    shtml.Append("<td class=\"ConsignesTd_Name\">详细地址：</td>");
                    shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userAddr\" id=\"userAddr\" readonly value=\"" + AddressInfo + "\"></td>");
                    shtml.Append("<td class=\"ConsignesTd_Name\">邮政编码：</td>");
                    shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userZip\" id=\"userZip\" readonly value=\"" + post + "\"></td>");
                    shtml.Append("</tr>");
                    shtml.Append("<tr>");
                    shtml.Append("<td class=\"ConsignesTd_Name\">电话：</td>");
                    shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userPhone\" id=\"userPhone\" readonly value=\"" + HomePhone + "\"></td>");
                    shtml.Append("<td class=\"ConsignesTd_Name\">手机：</td>");
                    shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"userMobile\" id=\"userMobile\" readonly value=\"" + HomePhone + "\"></td>");
                    shtml.Append("</tr>");
                    shtml.Append("<tr>");
                    shtml.Append("<td class=\"ConsignesTd_Name\">标志建筑：</td>");
                    shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"ConstructionSigns\" id=\"ConstructionSigns\" readonly value=\"" + constructionSigns + "\"></td>");
                    shtml.Append("<td class=\"ConsignesTd_Name\">最佳送货时间：</td>");
                    shtml.Append("<td class=\"ConsignesTd_Value\"><input type=\"text\" name=\"ConsigneTime\" id=\"ConsigneTime\" readonly value=\"" + consignesTime + "\"></td>");
                    shtml.Append("</tr>");
                    shtml.Append("<tr><td colspan=\"4\" class=\"ConsignsOperate\"><input type=\"hidden\" name=\"isFillConsignedata\" id=\"isFillConsignedata\" value=\"" + fillConsignedata + "\" />" + operate + "</td></tr>");
                    shtml.Append("</table>");
                    shtml.Append("</div>");
                    mystyle = mystyle.Replace("[#ConsigneeInfo]", shtml.ToString());

                }
                #endregion

                #region 在线支付信息
                if (mystyle.IndexOf("[#OnLinePaymentInfo]") > -1)
                {
                    StringBuilder shtmlPOI = new StringBuilder();
                    YXShop.BLL.SystemInfo.TerraceManage tmbll = new YXShop.BLL.SystemInfo.TerraceManage();
                    string UserTypeId = ChangeHope.WebPage.PageRequest.GetQueryString("UserTypeId");
                    string UserId = ChangeHope.WebPage.PageRequest.GetQueryString("UserId");
                    ChangeHope.DataBase.DataByPage dbp = null;
                    if (sp.IsPayMentWebOperation)
                    {
                        dbp = tmbll.GetList(" [order by] payment_id asc", 10, " and payment_putouttypeid=0");
                    }
                    else
                    {

                        if (UserTypeId == "0")
                        {
                            dbp = tmbll.GetList(" [order by] payment_id asc", 10, " and payment_putouttypeid=0");
                        }
                        else
                        {
                            dbp = tmbll.GetList(" [order by] payment_id asc", 10, " and payment_putouttypeid=1 and payment_putoutid=" + UserId + "");
                        }
                    }
                    if (dbp.DataReader != null)
                    {
                        shtmlPOI.Append("<div id=\"divPaymentMode\">");
                        shtmlPOI.Append("<table class=\"PaymentModeTable\">");
                        shtmlPOI.Append("<tr>");
                        shtmlPOI.Append("<th class=\"PaymentModeTitle1\"></th>");
                        shtmlPOI.Append("<th class=\"PaymentModeTitle2\">名称</th>");
                        shtmlPOI.Append("<th class=\"PaymentModeTitle3\">订购描述</th>");
                        shtmlPOI.Append("<th class=\"PaymentModeTitle4\">手续费</th>");
                        shtmlPOI.Append("</tr>");
                        while (dbp.DataReader.Read())
                        {
                            shtmlPOI.Append("<tr>");
                            shtmlPOI.Append("<td><input type=\"radio\" name=\"paymentMethod\" id=\"paymentMethod\" value=\"" + dbp.DataReader["payment_id"].ToString() + "|" + dbp.DataReader["payment_expenses"].ToString() + "\" onclick=\"PaymentCost('" + Convert.ToDouble(dbp.DataReader["payment_expenses"]).ToString("f2") + "')\" /></td>");
                            shtmlPOI.Append("<td>" + dbp.DataReader["payment_name"].ToString() + "</td>");
                            shtmlPOI.Append("<td>" + dbp.DataReader["payment_description"].ToString() + "</td>");
                            shtmlPOI.Append("<td>" + Convert.ToDouble(dbp.DataReader["payment_expenses"]).ToString("f2") + "</td>");
                            shtmlPOI.Append("</tr>");
                        }
                        shtmlPOI.Append("</table>");
                    }
                    mystyle = mystyle.Replace("[#OnLinePaymentInfo]", shtmlPOI.ToString());
                }
                #endregion

          

                #region 购物车信息
                ProfileCommon Profile = new ProfileCommon();
                ICollection<CartItemInfo> cart = Profile.ShoppingCart.CartItems;
                double productTotalPrice = 0;
                double productTotalWeight = 0;
                double productTotalIntegral = 0;
                
                int productTotalCount = 0;
                YXShop.BLL.Product.ProductInfo productbll = new YXShop.BLL.Product.ProductInfo();
                YXShop.BLL.Product.ProductSparepart spabll = new YXShop.BLL.Product.ProductSparepart();
                YXShop.BLL.Product.ProductSpecification spebll = new YXShop.BLL.Product.ProductSpecification();
                Common dp = new Common();
                foreach (CartItemInfo info in cart)
                {
                    YXShop.Model.Product.ProductInfo productmodel = productbll.GetModel(int.Parse(info.ProductId));
                    if (productmodel != null)
                    {
                        double price = double.Parse(productmodel.pro_ShopPrice.ToString());
                        //配件
                        string fittingProductInfo = "";
                        int fittingProductCount = 0;
                        double fittingProductWeight = 0;
                        double fittingProductIntergal = 0;
                        double fittingTotalPrice = 0;
                        YXShop.Common.ProductInfo.FittingDisposal(info.SparepartId, info.FittingsProductId, info.FittingsProductCount, out fittingProductCount, out fittingProductInfo, out fittingTotalPrice, out fittingProductWeight, out fittingProductIntergal);
                        //规格
                        string memberPrice = productmodel.pro_RatingDiscount;
                        string productNo = productmodel.pro_NO;

                        if (!string.IsNullOrEmpty(productmodel.pro_Specifications.Trim()))
                        {
                            YXShop.Common.ProductInfo.SpecificationDisposal(Convert.ToInt32(info.ProductId), info.Specification, memberPrice, price, out price, out productNo);
                        }
                        double count = 0;
                        count = double.Parse(info.Quantity.ToString());
                        double total = (price * count) + fittingTotalPrice;
                        productTotalPrice += total;
                        productTotalCount += Convert.ToInt32(count) + fittingProductCount;
                        productTotalWeight += Convert.ToDouble(productmodel.pro_Weight) + fittingProductWeight;
                        productTotalIntegral += (Convert.ToDouble(productmodel.pro_DonateIntegral)*info.Quantity )+ fittingProductIntergal;
                    }
                }

                if (mystyle.Contains("[#Pro_TotalPrice]"))
                {
                    mystyle = mystyle.Replace("[#Pro_TotalPrice]", productTotalPrice.ToString("f2"));
                }
                if (mystyle.Contains("[#Pro_TotalProductCount]"))
                {
                    mystyle = mystyle.Replace("[#Pro_TotalProductCount]", productTotalCount.ToString());
                }
                if (mystyle.Contains("[#Pro_TotalWeight]"))
                {
                    mystyle = mystyle.Replace("[#Pro_TotalWeight]", productTotalWeight.ToString());
                }
                if (mystyle.Contains("[#Pro_TotalInter]"))
                {
                    mystyle = mystyle.Replace("[#Pro_TotalInter]", productTotalIntegral.ToString("f2"));
                }
                #endregion

                #region 陪送信息
                if (mystyle.IndexOf("[#EscortInfo]") > -1)
                {
                    int uid = 0;
                    if (HttpContext.Current.Session["MemberID"] != null)
                    {
                        uid = Convert.ToInt32(HttpContext.Current.Session["MemberID"]);
                    }

                    string provinces = "";
                    string city = "";
                    string borough = "";
                    YXShop.BLL.Member.ReceAddress rabll = new YXShop.BLL.Member.ReceAddress();
                    ChangeHope.DataBase.DataByPage db = rabll.GetList(" uid=" + uid + " and stat=1");
                    if (db.DataReader != null)
                    {
                        if (db.DataReader.Read())
                        {
                            provinces = db.DataReader["province"].ToString();
                            city = db.DataReader["city"].ToString();
                            borough = db.DataReader["borough"].ToString();
                        }
                    }
                    else
                    {
                        YXShop.BLL.Member.MemberInfo mibll = new YXShop.BLL.Member.MemberInfo();
                        YXShop.Model.Member.MemberInfo mimodel = mibll.GetModel(uid);
                        if (mimodel != null)
                        {
                            provinces = mimodel.Province;
                            city = mimodel.City;
                            borough = mimodel.Borough;
                        }
                    }
                    StringBuilder shtmldi = new StringBuilder();
                    shtmldi.Append("<div id=\"divDeliveryMode\">");
                    shtmldi.Append("<table id=\"DeliveryModeTable\" name=\"DeliveryModeTable\" class=\"DeliveryModeTable\" align=\"center\" border=\"0\" cellpadding=\"5\" cellspacing=\"1\">");
                    shtmldi.Append("<tr>");
                    shtmldi.Append("<th class=\"DeliveryModeTitle1\"></th>");
                    shtmldi.Append("<th class=\"DeliveryModeTitle2\">名称</th>");
                    shtmldi.Append("<th class=\"DeliveryModeTitle3\">订购描述</th>");
                    shtmldi.Append("<th class=\"DeliveryModeTitle4\">费用</th>");
                    shtmldi.Append("<th class=\"DeliveryModeTitle5\">免费额度</th>");
                    shtmldi.Append("<th class=\"DeliveryModeTitle6\">保价费用</th>");
                    shtmldi.Append("</tr>");


                    YXShop.BLL.SystemInfo.PostArea postarea = new YXShop.BLL.SystemInfo.PostArea();
                    List<YXShop.Model.SystemInfo.PostArea> list = null;
                    YXShop.BLL.SystemInfo.Deliver deliver = new YXShop.BLL.SystemInfo.Deliver();
                    YXShop.Model.SystemInfo.Deliver objdeli = null;
                    string UserTypeId = ChangeHope.WebPage.PageRequest.GetQueryString("UserTypeId");
                    string UserId = ChangeHope.WebPage.PageRequest.GetQueryString("UserId");
                    if (provinces != "" || city != "" || borough != "")
                    {
                        //如果支付给网站运营商：
                        if (sp.IsPayMentWebOperation)
                        {
                            if (borough != string.Empty && borough != "")
                            {
                                list = postarea.GetPostMethodByAreaId(borough, "  putouttyid=0");
                                if (list.Count == 0)
                                {
                                    list = postarea.GetPostMethodByAreaId(city, " putouttyid=0");
                                    if (list.Count == 0)
                                    {
                                        list = postarea.GetPostMethodByAreaId(provinces, " putouttyid=0");
                                    }
                                }

                            }
                        }
                        else
                        {
                            if (borough != string.Empty && borough != "")
                            {
                                if (UserTypeId == "0")
                                {
                                    list = postarea.GetPostMethodByAreaId(borough, "  putouttyid=0");
                                    if (list.Count == 0)
                                    {
                                        list = postarea.GetPostMethodByAreaId(city, " putouttyid=0");
                                        if (list.Count == 0)
                                        {
                                            list = postarea.GetPostMethodByAreaId(provinces, " putouttyid=0");
                                        }
                                    }
                                }
                                else
                                {
                                    list = postarea.GetPostMethodByAreaId(borough, "  putouttyid=" + UserTypeId + " and putoutid=" + UserId + " ");
                                    if (list.Count == 0)
                                    {
                                        list = postarea.GetPostMethodByAreaId(city, " putouttyid=" + UserTypeId + " and putoutid=" + UserId + "");
                                        if (list.Count == 0)
                                        {
                                            list = postarea.GetPostMethodByAreaId(provinces, " putouttyid=" + UserTypeId + " and putoutid=" + UserId + "");
                                        }
                                    }
                                }
                            }
                        }
                        if (list.Count > 0)
                        {
                            foreach (YXShop.Model.SystemInfo.PostArea objpost in list)
                            {
                                string delivery = YXShop.Common.OrderInfo.TotalFreight(Convert.ToInt32(objpost.Id), float.Parse(productTotalWeight.ToString()), float.Parse(productTotalPrice.ToString()), productTotalCount);
                                objdeli = deliver.GetModelByID(objpost.Deliverymode);
                                shtmldi.Append("<tr>");
                                shtmldi.Append("<td><input type=\"radio\" id=\"DeliveryMode\" name=\"DeliveryMode\" value=\"" + objpost.Id.ToString() + "\" onclick=\"DeliveryCost('" + delivery + "')\" /></td>");
                                shtmldi.Append("<td>" + objdeli.Distributionname + "</td>");
                                shtmldi.Append("<td>" + objdeli.Distributiondescription + "</td>");
                                shtmldi.Append("<td>￥" + delivery + "元</td>");
                                shtmldi.Append("<td>￥" + Convert.ToDouble(objpost.Freeamount).ToString("f2") + "元</td>");
                                string insuredCosts = objdeli.Insuredcosts.ToString();
                                if (insuredCosts == "0")
                                {
                                    insuredCosts = "不支持保价";
                                }
                                else
                                {
                                    insuredCosts = objdeli.Insuredcosts.ToString() + "%";
                                }
                                shtmldi.Append("<td>" + insuredCosts + "</td>");
                                shtmldi.Append("</tr>");
                            }
                        }

                    }
                    shtmldi.Append("</table>");
                    shtmldi.Append("</div>");
                    mystyle = mystyle.Replace("[#EscortInfo]", shtmldi.ToString());
                }

                #endregion

                #region 费用总计
                if (mystyle.Contains("[#Pro_TotalCost]"))
                {
                    StringBuilder shtmlCost = new StringBuilder();
                    shtmlCost.Append("<table class=\"TotalCostTable\">");
                    shtmlCost.Append("<tr>");
                    shtmlCost.Append("<td>该订单完成后，您将获得 " + productTotalIntegral + " 积分</td>");
                    shtmlCost.Append("</tr>");
                    shtmlCost.Append("<tr>");
                    shtmlCost.Append("<td>");
                    shtmlCost.Append("<input type=\"hidden\" name=\"productTotalPrice\" id=\"productTotalPrice\" value=\"" + productTotalPrice.ToString("f2") + "\" />");
                    shtmlCost.Append("<input type=\"hidden\" name=\"freightTotalPrice\" id=\"freightTotalPrice\" />");
                    shtmlCost.Append("<input type=\"hidden\" name=\"FeeTotalPrice\" id=\"FeeTotalPrice\" />");
                    shtmlCost.Append("<span id=\"TotalPriceInfo\">商品总价: ￥" + productTotalPrice.ToString("f2") + "元</span></td>");
                    shtmlCost.Append("</tr>");
                    shtmlCost.Append("<tr>");
                    shtmlCost.Append("<td>应付款金额: ￥<span id=\"TotalCostMoney\">" + productTotalPrice.ToString("f2") + "</span>元</td>");
                    shtmlCost.Append("</tr>");
                    shtmlCost.Append("</table>");
                    mystyle = mystyle.Replace("[#Pro_TotalCost]", shtmlCost.ToString());
                }

                #endregion
            }
            return mystyle;
        }
    }
}
